package com.junyidao.game.controller;

import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.pojo.SysUser;
import com.junyidao.game.pojo.TGames;
import com.junyidao.game.service.GameService;
import com.junyidao.game.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Map;

/**
 * Created by zzh on 2016/6/29.
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private GameService gameService;

    @RequestMapping
    public ModelAndView login() {
        ModelAndView result = new ModelAndView("login");
        LoginInfo.removeUser();
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/checkLogin")
    @Action(operType = OperLogConstant.LOGIN, desc = "登录", isLogin = true)
    public Map<String, Object> checkLogin(String accountName, String password) {
        String firstUrl = userService.checkLogin(accountName, password);
        return resultSucess(firstUrl);
    }

    @RequestMapping(value = "/checkRegister")
    public ModelAndView checkRegister() {
        ModelAndView result = new ModelAndView("wait_check");
        SysUser user = LoginInfo.getUser();
        result.addObject("user", user);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/listNewGame")
    @Action(desc = "获取最新游戏列表", sysPriv = false)
    public Map<String, Object> listNewGame(TGames games) {
        Map<String, Object> map = gameService.getNewGame(games);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/listPopularGame")
    @Action(desc = "获取热门游戏列表", sysPriv = false)
    public Map<String, Object> listPopularGame(TGames games) {
        Map<String, Object> map = gameService.getPopularGame(games);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/listServiceGame")
    @Action(desc = "获取最新开服列表", sysPriv = false)
    public Map<String, Object> listServiceGame(TGames games) {
        Map<String, Object> map = gameService.getServiceGame(games);
        return resultSucess(map);
    }

    @RequestMapping(value = "/logo/{id}")
    @Action(desc = "游戏LOGO", sysPriv = false)
    public void logo(@PathVariable Integer id, HttpServletResponse response) throws Exception {
        TGames games = gameService.getGameById(id);
        OutputStream out = response.getOutputStream();
        byte[] bytes = games.getGameLogo();
        out.write(bytes);
        out.flush();
        out.close();
    }
}
